import {
    shake,
    alert
} from '../../../utils/util'
import {
    getPeopleList,
    subtractInsure
} from '../../api/long-insure'
import user from '../../../utils/user'
import createRecycleContext from 'miniprogram-recycle-view'
import dayjs from 'dayjs'
var app = getApp()
let ctx
Page({
    data: {
        code: '',
        companyPlanId: '',
        peoples: [],
        searchTypes: ['员工', '被派遣单位'],
        searchType: 0,
        search: '',
        type: '',
        back: '',
        selects: [],
        date: '', // 在保时间,
        sxDis: false,
        insurCompany: '',
        selectAll: false,
        selectDate: '',
        isSelectDate: false,
        totalCount: '',
        isSelected: false
    },
    static: {
        pageNum: 1,
        cloneArr: [], // 暂存所有数据
    },
    onChangeDate() {
        if (this.data.code === 'period_015_gwy') {
            wx.showToast({
                title: '此方案减保时间不可修改',
                icon: 'none'
            })
        }
    },
    async onLoad(options) {
        await user.waitLogin()
        console.log(options, 'options')
        // sx保险方案加减保时间限制  period_015_gwy
        if (options.code === 'period_015_gwy') {
            this.setData({
                sxDis: true,
                selectDate: dayjs().format('YYYY-MM-DD')
            })
        } else {
            this.setData({
                sxDis: false
            })
        }
        if (options.company) {
            this.setData({
                insurCompany: options.company
            })
        }
        if (options.type) {
            this.setData({
                type: options.type,
                back: options.back || '',
                date: options.date || ''
            })
        }
        if (options.id) {
            this.setData({
                companyPlanId: options.id
            })
        }
        this.setData({
            companyPlanId: options.id,
            code: options.code,
            selectDate: dayjs().format('YYYY-MM-DD')
        })
    },
    async onShow() {
        await user.waitLogin()
        if (app.version === '暂无' && Number(app.version) < Number('2.2.1')) {
            alert('当前微信版本过低，请更新微信版本')
            console.log('当前微信版本过低，请更新微信版本')
        }
        console.log(app.version, 'app.version')
        ctx = createRecycleContext({
            id: 'recycleId',
            dataKey: 'peoples',
            page: this,
            itemSize: { // 这个参数也可以直接传下面定义的 this.itemSizeFunc 函数
                width: (wx?.getWindowInfo && wx.getWindowInfo()?.screenWidth || 750) / 750 * 690,
                height: (wx?.getWindowInfo && wx.getWindowInfo()?.screenWidth || 750) / 750 * 160
            }
        })
        console.log(ctx.comp.sizeArray, 'sizeArray')
        ctx.splice(0, ctx.comp.sizeArray.length)
        await this.getPeople(1)
    },
    onReachBottom() {
        // this.getPeople()
    },
    onSearch(event) {
        console.log(event, 'event搜索')
        const {
            value
        } = event.detail
        this.setData({
            search: value,
            selectAll: false
        })
        console.log(this.data.search, 'search')
        shake(() => {
            ctx.splice(0, ctx.comp.sizeArray.length)
            this.getPeople(1)
        })
    },
    async getPeople(clear) {
        const result = await getPeopleList({
            companyPlanId: this.data.companyPlanId,
            pageNum: this.static?.pageNum,
            pageSize: 2000,
            searchUserInfo: this.data.searchType ? '' : this.data.search,
            searchServeCompany: this.data.searchType ? this.data.search : '',
            outDangerDate: this.data.date || '',
            insurCompany: this.data.insurCompany,
            insurValidFlag: false,
            insurEmptyEndDateFlag: true
        })
        ctx.append(result.list)
        console.log(this.static.cloneArr, 'cloneArr')
        if (clear) {
            this.static.cloneArr = result.list
            console.log(this.static.cloneArr, '111111111111111111111111111111')
            this.setData({
                totalCount: result.totalCount
            })
        }
    },
    onTypeChange(event) {
        console.log(event, 'event')
        this.setData({
            searchType: Number(event.detail.value)
        })
    },
    onSelect(event) {
        const index = event.currentTarget.dataset.index
        const worker = this.data.peoples[index]
        console.log(index, worker, '单选内容')
        const indexed = this.data.selects.filter(item => item.id === worker.id)
        const ind = this.data.selects.findIndex(item => {
            return item.id === worker.id
        })
        console.log(indexed, 'indexindex')
        if (indexed.length === 0) {
            this.data.selects.push(worker)
        } else {
            this.data.selects.splice(ind, 1)
            console.log(this.data.selects)
        }
        this.setData({
            selects: Array.from(new Set(this.data.selects))
        })
        this.setData({
            selectAll: this.data.selects.length === this.data.peoples.length ? true : false
        })
        console.log(this.data.selects, '赋值')
    },
    onSubmit() {
        this.setData({
            isSelectDate: true
        })
    },
    onSelectAll(event) {
        console.log(event, 'event')
        const {
            type
        } = event.currentTarget.dataset
        this.setData({
            selectAll: type == 1 ? false : true
        })
        if (type == 1) {
            this.setData({
                selects: []
            })
        } else {
            this.setData({
                // selects:Array.from(new Set([...this.data.selectsPay,...this.data.selectsRemarks,...this.data.selectsInvitee]))
                // this.static.cloneArr
                selects: Array.from(new Set(this.data.peoples))
            })
        }
        console.log(this.data.selects.length, this.data.peoples.length, this.data.selectAll, 'selects')
    },
    onDate(event) {
        console.log(event, 'event')
        const {
            value
        } = event.detail
        this.setData({
            selectDate: value
        })

    },
    // 弹窗取消
    onPopupCancel() {
        this.setData({
            isSelectDate: false,
            isSelected: false
        })
    },
    // 弹窗确定
    onPopupAdd() {
        this.setData({
            isSelectDate: false,
            isSelected: true
        })
    },
    // 取消操作
    onCancelOperation() {
        this.setData({
            isSelected: false
        })
    },
    // 确认减保
    async onConfirmReduce() {
        console.log(this.data.selects, 'selects')
        console.log(this.data.selectDate, 'selectDate')
        const result = await subtractInsure({
            companyPlanId: this.data.companyPlanId,
            ids: this.data.selects.map(item => item.id),
            insurEndDay: this.data.selectDate
        })
        wx.redirectTo({
            url: '/pages/success/success?title=减保成功&content=本次减保共' + this.data.selects.length + '人，生效日期为' + this.data.selectDate,
        })
        console.log(result, 'result')
    }
})